package com.gitee.wsl.data.store

//import com.gitee.wsl.android.eventbus.SweetFlowBus


import com.gitee.wsl.io.api.getOrNull
import com.gitee.wsl.io.api.put
import com.gitee.wsl.io.api.remove
import com.gitee.wsl.prop.AttributeKey
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

class StateSavedNullAbleVar<T : Any>(val state: StoreServer,
                                     val key:AttributeKey<T>,
                                     val sampleValue:T?,
): ReadWriteProperty<Any, T?> {

    override fun getValue(thisRef: Any, property: KProperty<*>): T? {
        return state.getOrNull(key)?:sampleValue
    }

    override fun setValue(thisRef: Any, property: KProperty<*>, value: T?) {
        if(value == null)
            state.remove(key)
        else
            state.put(key, value)
    }

}

inline fun <reified T:Any> StateSavedNullAbleVar(state: StoreServer, key:String, defaultValue:T?) = StateSavedNullAbleVar(state,AttributeKey(key), defaultValue)